﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Cooksta_Data
{
    /// <summary>
    /// 
    /// </summary>
    public static class DataAccess
    {
        public static DataTable Get_Data
        (string connectionString, string commandText,
        CommandType commandType, SqlParameter[] param)
        {
            SqlConnection cn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            try
            {
                cn.ConnectionString = connectionString;
                cmd.CommandText = commandText;
                cmd.CommandType = commandType;
                cmd.Connection = cn;
                adapter.SelectCommand = cmd;

                if (param != null)
                {
                foreach (SqlParameter sp in param)
                {
                cmd.Parameters.Add(sp);
                }
                }

                cn.Open();
                adapter.Fill(dt);
                cn.Close();
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            finally
            {
                cn.Dispose();
                cmd.Dispose();
                adapter.Dispose();
            }
        }

        public static void Send_Data
        (string connectionString, string commandText,
        CommandType commandType, SqlParameter[] param)
        {
            SqlConnection cn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();

            try
            {
                cn.ConnectionString = connectionString;
                cmd.CommandText = commandText;
                cmd.CommandType = commandType;

            if (param != null)
            {
                foreach (SqlParameter sp in param)
                {
                    cmd.Parameters.Add(sp);
                }
            }
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            finally
            {
                cn.Dispose();
                cmd.Dispose();
            }
        }

        public static object Insert_Data(
        string connectionString, string commandText,
        CommandType commandType, SqlParameter[] param)
        {
            SqlConnection cn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();

            try
            {
                cn.ConnectionString = connectionString;
                cmd.CommandText = commandText;
                cmd.CommandType = commandType;

            if (param != null)
            {
                foreach (SqlParameter sp in param)
                {
                    cmd.Parameters.Add(sp);
                }
            }
                cn.Open();
                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            finally
            {
                cn.Close();
                cn.Dispose();
                cmd.Dispose();
            }
        }
    }
}
